<template>
  <div class="pagination-container" :style="{ display: isShow ? 'block' : 'none' }">
    <el-pagination
      :isShow="isShow"
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    //是否显示分页器
    isShow: {
      type: Boolean,
      default: true
    },
    //分页按钮背景颜色
    background: {
      type: Boolean,
      default: true
    },
    //总条目数
    total: {
      required: true,
      type: Number
    },
    //每页显示条目个数
    limit: {
      type: Number,
      default: 10
    },
    //当前页数
    page: {
      type: Number,
      default: 1
    },

    //每页显示个数选择器的选项设置
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50];
      }
    },
    //组件布局，子组件名用逗号分隔
    layout: {
      type: String,
      default: '->,total, sizes, prev, pager, next, jumper'
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit('update:page', val);
      }
    },
    pageSize: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit('update:limit', val);
      }
    }
  },
  methods: {
    // 每页条数改变时
    handleSizeChange(val) {
      // this.$emit('pagination', { page: this.currentPage, limit: val });
      this.$emit('pagination', { page: 1, limit: val });
    },
    // 页码改变时
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize });
    }
  }
};
</script>

<style scoped>
.pagination-container {
  margin-top: 15px;
}
</style>